import { openLink } from "./dingtalk";
import R from "./ramda";
import { toast } from "../utils/dingtalk";

// 跳转到外部应用的工具类
const getEnv = domain =>
  R.find(env => domain.indexOf(env) !== -1, ["pro", "pre", "daily"]);
const host = window.location.host;
const prefix = getEnv(host) ? getEnv(host) + "-" : "";

const idMapper = {
  aflow: (corpId, id) =>
    `https://${prefix}aflow.dingtalk.com/dingtalk/mobile/homepage.htm?corpid=${corpId}&dd_share=false&showmenu=true&dd_progress=false&back=native&procInstId=${id}&dd_web_timestamp=1502334889637.678#/approval`,
  log: corpId =>
    `https://${prefix}landray.dingtalkapps.com/alid/app/report/home.html?showmenu=false&dd_share=false&corpid=${corpId}`,
  signIn: corpId =>
    `https://${prefix}attendance.dingtalk.com/attendance/mobile/mapAttendance.html?showmenu=false&dd_progress=false&dd_share=false&corpid=${corpId}`,
  workHard: corpId =>
    `https://${prefix}attend.dingtalk.com/attend/index.html?corpId=${corpId}&showmenu=false&dd_share=false&rankTab=hard#stat/rank`,
  attend: corpId =>
    `https://${prefix}attend.dingtalk.com/attend/index.html?corpId=${corpId}&showmenu=false&dd_share=false&#stat`
};

// 空子欠一个log 的 from 参数，楼上欠一个根据from 参数返回我们页面功能（allan已加）
// xuqi   欠 attend 页面 from

const userMapper = {
  attend: ({ corpId, userId }) =>
    `https://${prefix}attend.dingtalk.com/attend/index.html?showmenu=false&swfrom=hrm&dd_progress=false&dd_share=false&corpId=${corpId}&userId=${userId}&date=${new Date().getTime()}#user/attendSummaryNew`,
  signIn: ({ corpId, userId }) =>
    `https://${prefix}attendance.dingtalk.com/attendance/mobile/personal.html?activity_from=hrm&corpid=${corpId}&staff_id=${userId}&type=personal&dd_share=false&showmenu=false&dd_progress=false`,
  holiday: ({ corpId, userId, isSelf }) =>
    `https://${prefix}aflow.dingtalk.com/dingtalk/mobile/homepage.htm?corpid=${corpId}&swfrom=${isSelf ? 'hrmPerson' : 'hrm'}&userId=${userId}&dd_share=false&showmenu=true#/leaveBalance`,
  aflow: ({ corpId, userId, username }) =>
    `https://${prefix}aflow.dingtalk.com/dingtalk/mobile/homepage.htm?corpid=${corpId}&dd_share=false&userId=${userId}&swfrom=hrm&userName=${username}&showmenu=true#/applied`,
  log: ({ corpId, userId, username, ismy = 1 }) =>
    `https://${prefix}landray.dingtalkapps.com/alid/app/report/received_new.html?ismy=${ismy}&fromHeadClick=${username}&searchKey=${userId}&swfrom=work_hrm&corpid=${corpId}&dd_share=false`
};
export default function navigate({
  system,
  corpId,
  isSelf,//是否是自己
  id,
  userId,
  username,
  ismy
}) {
  // 系统不能为空，corpId不能为空。 userId 和 id 不能同时为空

  if (!system || !corpId || (!id && !userId))
    return toast({
      text: "参数不全"
    });
  if (!idMapper[system] && !userMapper[system])
    return toast({
      text: "找不到对应的系统"
    });

  if (id) {
    // 已记录为纬度
    console.info(idMapper[system](corpId, id)); // eslint-disable-line
    return openLink(idMapper[system](corpId, id));
  }
  // 人的纬度
  console.info(userMapper[system]({ corpId, userId, username, isSelf, ismy })); // eslint-disable-line
  return openLink(userMapper[system]({ corpId, userId, username, isSelf, ismy }));
}
